package data.admin;

import dao.SQLiteConnection;
import gui.admin.AlteWin;
import javax.swing.*;
import java.sql.*;

public class Alte extends AlteWin {

    String ISBN;
    String bookname;
    String booktype;
    String publisher;
    String author;
    String number;

    Alte(String ms_ISBN,String ms_bookname,String ms_booktype,String ms_publisher,
         String ms_author,String ms_number){
        //传入参数
        this.ISBN=ms_ISBN;
        this.bookname=ms_bookname;
        this.booktype=ms_booktype;
        this.publisher=ms_publisher;
        this.author=ms_author;
        this.number=ms_number;
        //自动输入参数
        getT1().setText(ISBN);
        getT2().setText(bookname);
        getT3().setText(booktype);
        getT4().setText(publisher);
        getT5().setText(author);
        getT6().setText(number);

        //修改按钮
        getB1().addActionListener(e->{
            //获取文本框的内容
            String ISBN = getT1().getText();
            String bookname = getT2().getText();
            String booktype = getT3().getText();
            String publisher = getT4().getText();
            String author = getT5().getText();
            String number = getT6().getText();

            SQLiteConnection Connection=new SQLiteConnection();
            Connection.Connect_SQLite();
            Connection conn=Connection.getConnection();
            try {
                // 生成SQL更新语句
                String sql = "UPDATE Book SET bookname = ?, booktype = ?, publisher = ?, author = ?, number = ? WHERE ISBN = ?";
                // 创建PreparedStatement对象，并设置参数
                PreparedStatement stmt = conn.prepareStatement(sql);
                stmt.setString(1, bookname);
                stmt.setString(2, booktype);
                stmt.setString(3, publisher);
                stmt.setString(4, author);
                stmt.setString(5, number);
                stmt.setString(6, ISBN);

                // 执行更新语句
                int rowsAffected = stmt.executeUpdate();
                if (rowsAffected > 0) {
                    // 更新成功
                    JOptionPane.showMessageDialog(this,"修改成功");
                    System.out.println("Book表数据已成功修改");
                } else {
                    // 更新失败
                    JOptionPane.showMessageDialog(this,"修改失败");
                    System.out.println("修改Book表数据失败");
                }
                // 关闭PreparedStatement对象
                stmt.close();
                this.dispose();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }finally {
                Connection.closeConnection();
            }
        });
    }
}
